package com.huonan.jb.tools.fileuse.fitdata;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description TODO
 * @Author lxp
 * @Date 2023/3/31 下午2:08
 * @Version V1.0
 **/
@Slf4j
public class App {

    public static void main(String[] args) {
        //  String inFile = "/Users/lxp/Documents/dev/数据需求/运营/数据修复/huichuan/douyin_user_snapshot_20230324.txt";
        String inFile = "/Users/lxp/Documents/dev/数据需求/运营/数据修复/huichuan/douyin_user_20230324.txt";
        String outFile = "/Users/lxp/Documents/dev/数据需求/运营/数据修复/huichuan/fit_douyin_user_20230324.txt";

        String userIndex = "user_v2_douyin";
        List<String> list = FileUtil.readLines(inFile, "UTF-8");
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for (String line : list) {
            JSONObject soureobject = JSONUtil.parseObj(line);
            String id = soureobject.getStr("kw_uid");
            JSONObject newObj = new JSONObject();
            handle(soureobject, id, newObj, "object_bussinessInteractionRate", "object_bussinessInteractionRate");
            handle(soureobject, id, newObj, "object_bussinessVideoViewRate", "object_bussinessVideoViewRate");
            handle(soureobject, id, newObj, "object_interactionRate", "object_interactionRatePlus");
            handle(soureobject, id, newObj, "object_videoViewRate", "object_videoViewRate");
            if (newObj.size() >= 1) {
                count++;
                newObj.putOnce("_id", id);
                newObj.putOnce("_index", userIndex);
                String jsonStr = JSONUtil.toJsonStr(newObj);
                sb.append(jsonStr).append("\n");
            }
        }
        log.info("数据总量: {},需要修复数据量: {}", list.size(), count);
        if (sb.length() > 1) {
            sb.deleteCharAt(sb.length() - 1);
        }
        FileUtil.writeUtf8String(sb.toString(), outFile);
    }

    private static void handle(JSONObject object, String id, JSONObject newObj, String sKey, String dkey) {
        if (!object.containsKey(sKey)) {
            newObj.append(dkey, null);
        } else {
            JSONArray bussinessVideoViewRate = object.getJSONArray(sKey);
            getObjValue(id, newObj, bussinessVideoViewRate, dkey);
        }
    }

    private static void getObjValue(String id, JSONObject newObj, JSONArray bussinessVideoViewRate, String newObjKey) {
        try {
            for (Object obj : bussinessVideoViewRate) {
                JSONObject jsonObject = (JSONObject) obj;
                Double doubleRate = jsonObject.getDouble("double_rate");

                if (doubleRate != null && doubleRate > 1) {
                    log.info("id:{},double_rate>1", id);
                    System.exit(0);
                }
                newObj.append(newObjKey, jsonObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("exception");
        } finally {

        }
    }
}
